import cv
import numpy as np
import cPickle
import sys


def mapIndex(lenA, lenB, idx):
    ratio = float(lenA)/lenB
    return int(round(ratio*idx))
    

dfile = open(sys.argv[1]+'.depth','r')
#ifile = open(sys.argv[1]+'_image.data','r')

print "Loading data..."
depthDataArray = cPickle.load(dfile)

#rgbDataArray = cPick#le.load(ifile)
print "Done!"

cv.NamedWindow('Depth')
#cv.NamedWindow('RGB')
#print "depthData length: ",len(depthDataArray)
#print "rgbData length: ",len(rgbDataArray)

for i in range(0,len(depthDataArray)):
    depthData = np.array(depthDataArray[i])
    

    #depthData -= np.min(depthData.ravel())
    #depthData *= 65536 / np.max(depthData.ravel())

    depthData = depthData.astype('uint8')
    depthData = depthData * 20000
    image = cv.CreateImageHeader((depthData.shape[1], depthData.shape[0]), cv.IPL_DEPTH_8U,1)
    cv.SetData(image, depthData.tostring(), depthData.dtype.itemsize * depthData.shape[1])

    cv.ShowImage('Depth', image)
    cv.WaitKey(400)

   # rgbData = rgbDataArray[mapIndex(len(rgbDataArray),len(depthDataArray),i)]

    #image = cv.CreateImageHeader((rgbData.shape[1], rgbData.shape[0]), cv.IPL_DEPTH_8U,3)
    # Note: We swap from RGB to BGR here
    #cv.SetData(image, rgbData[:, :, ::-1].tostring(), rgbData.dtype.itemsize * 3 * rgbData.shape[1])
    #cv.ShowImage('RGB', image)
    #cv.WaitKey(40)
